/*
Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
*/

class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        vector<vector<int> >result;
        if (!numRows) return result;
        vector<int> cur(1,1), next;
        result.push_back(cur);
        if (numRows == 1) return result;
        for (int i = 2; i <= numRows; i++) {
            int len = cur.size();
            next.push_back(cur[0]);
            for (int k = 0; k < len-1; k++) {
                next.push_back(cur[k] + cur[k+1]);
            }
            next.push_back(cur[len-1]);
            result.push_back(next);
            swap(cur, next); next.clear();
        }
        return result;
    }
};
